package shop.yaojianfeng.springlearning.section6.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import shop.yaojianfeng.springlearning.section6.entity.Account;
import shop.yaojianfeng.springlearning.section6.dao.AccountDao;
import shop.yaojianfeng.springlearning.section6.service.AccountService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 简单账户表(Account)表服务实现类
 *
 * @author 
 * @since 2022-04-27 16:35:23
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private JdbcTemplate template;


    @Override
    public Account out(Account param) {
        String sql = "update account set money = money - ? where name = ?";
        template.update(sql,param.getMoney(),param.getName());
        return getByName(param);
    }

    @Override
    public Account in(Account param) {
        String sql = "update account set money = money + ? where name = ?";
        template.update(sql,param.getMoney(),param.getName());
//        System.out.println(20/0);
        return getByName(param);
    }

    @Override
    public void transfer(String outName, String inName, int money) {
        Account out = new Account(outName,money);
        Account in = new Account(inName,money);
        this.out(out);
        //制造异常 中断操作
//        System.out.println(10/0);
        this.in(in);
    }


    private Account getByName(Account param){
        return template.queryForObject("select * from account where name = ?"
        ,new BeanPropertyRowMapper<>(Account.class),param.getName());
    }
}
